package grails.company

import grails.converters.JSON

class ProjectController {

    def list() {
        def projects = Project.list()
        if (projects) {
            render(contentType: 'text/json') {
                success = true
                data = Project.list().collect {
                    [
                            'id': it.id,
                            'title': it.title,
                            'description': it.description,
                            'parentId': it?.department?.id
                    ]
                }
            }
        } else {
            render(contentType: 'text/json') { success = false }
        }
    }

    def view() {
        if (!params?.data?.id) {
            list()
            return
        }

        def prj = Project.findById(params.data.id)
        if (prj) {
            render(contentType: 'text/json') {
                success = true
                data = {
                    id = prj.id
                    title = prj.title
                    description = prj.description
                    parentId = prj.department.id
                }
            }
        } else {
            render(contentType: 'text/json') { success = false }
        }
    }

    def update() {
        if (!params?.data?.id || !params?.data?.title) {
            render(contentType: 'text/json') { success = false }
            return
        }

        def prj = Project.findById(params.data.id)
        if (!prj) {
            render(contentType: 'text/json') { success = false }
            return
        }

        prj.properties['title', 'description'] = params.data
        if (!prj.validate()) {
            render(contentType: 'text/json') { success = false }
            return
        }

        prj.save()

        render(contentType: 'text/json') { success = true }
    }

    def delete() {
        if (!params?.data?.id) {
            render(contentType: 'text/json') { success = false }
            return
        }

        def prj = Project.findById(params.data.id);
        if (prj) {
            prj.delete()
            render(contentType: 'text/json') { success = true }
        } else {
            render(contentType: 'text/json') { success = false }
        }
    }

    def create() {
        if (!params?.data?.parentId) {
            render(contentType: 'text/json') { success = false }
            return
        }

        def department = Department.findById(params.data.parentId)
        if (!department) {
            render(contentType: 'text/json') { success = false }
            return
        }

        def project = new Project();
        project.properties['title', 'description'] = params.data
        project.department = department

        if (!project.validate()) {
            render(contentType: 'text/json') { success = false }
            return
        }
        if (project.save()) {
            department.addToProjects(project)
            render(contentType: 'text/json') {
                success = true
                data = {
                    id = project.id
                    title = project.title
                    description = project.description
                    parentId = project.department.id
                }
            }
        } else {
            render(contentType: 'text/json') { success = false }
        }
    }
}
